<!--
 * @Description: 平台标准任务领取列表
 * @Author: liyujie
 * @Date: 2021/10/27
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/10/27
-->
<template>
    <PageLayout
        title="平台标准任务领取列表"
        class="standard-task-list-wrapper"
    >
        <template slot="filter">
            <zd-tabs
                :tab-list="tabsList"
                v-model="filterData.task_category"
                style="padding-left: 8px"
                @change="handleTabChange()"
            />
            <div class="filter-view">
                <div class="filter-item">
                    <div class="filter-item__title">
                        用户信息
                    </div>
                    <el-input
                        placeholder="搜索用户信息"
                        v-model="filterData.student_keyword"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="filter-item">
                    <p class="filter-item__title">
                        任务信息
                    </p>
                    <el-input
                        placeholder="搜索任务信息"
                        v-model="filterData.task_keyword"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="filter-item">
                    <p class="filter-item__title">
                        学校名称
                    </p>
                    <el-cascader
                        v-model="filterData.school_name"
                        placeholder="请选择学校"
                        :options="school_tree"
                        filterable
                        clearable
                        size="medium"
                        expand-trigger="click"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                        :props="{
                            label: 'name',
                            value: 'name',
                            children: 'children',
                            emitPath: false
                        }"
                    />
                </div>
                <div class="filter-item">
                    <p class="filter-item__title">
                        审核状态
                    </p>
                    <el-select
                        v-model="filterData.verify_status"
                        filterable
                        placeholder="请选择"
                        size="medium"
                        style="width: 152px"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in statusList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="filter-item">
                    <p class="filter-item__title">
                        奖励状态
                    </p>
                    <el-select
                        v-model="filterData.award_status"
                        filterable
                        placeholder="请选择"
                        size="medium"
                        style="width: 152px"
                        @change="getTableData(1)"
                    >
                        <el-option
                            label="全部"
                            :value="99"
                        />
                        <el-option
                            label="已发放"
                            :value="1"
                        />
                        <el-option
                            label="未发放"
                            :value="0"
                        />
                        <el-option
                            label="拒绝发放"
                            :value="-1"
                        />
                    </el-select>
                </div>
                <div class="filter-item">
                    <p class="filter-item__title">
                        领取时间
                    </p>
                    <el-date-picker
                        v-model="dateTimeRange"
                        type="datetimerange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        size="medium"
                        style="width: 360px"
                        value-format="timestamp"
                        @change="getTableData(1)"
                    />
                </div>
                <div class="filter-item">
                    <el-button
                        type="primary"
                        size="small"
                        class="query-button"
                        @click="getTableData(1)"
                    >
                        查询
                    </el-button>
                    <el-button
                        type="primary"
                        size="medium"
                        @click="handleExport()"
                    >
                        数据导出
                    </el-button>
                </div>
            </div>
        </template>
        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            :cell-style="{height: '106px'}"
            ref="table"
            class="data-table"
            v-loading="tableLoading"
        >
            <el-table-column
                label="序号"
                align="center"
                width="60"
                type="index"
                fixed="left"
                key="0"
            />
            <el-table-column
                label="用户信息"
                width="200"
                fixed="left"
                key="1"
            >
                <template slot-scope="scope">
                    <div class="cell-title">{{ scope.row.true_name ? scope.row.true_name : '--' }}</div>
                    <div class="cell-tips">手机号：{{ scope.row.phone }}</div>
                    <div
                        class="cell-tips"
                        v-if="scope.row.agent_level !== -1">
                        精英等级：{{ scope.row.agent_level > -1 ? `E${scope.row.agent_level}` : '--' }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="school_name"
                label="学校名称"
                show-overflow-tooltip
                align="left"
                width="140"
                :formatter="emptyFormatter"
                key="2"
            />
            <el-table-column
                label="任务信息"
                width="200"
                key="3"
            >
                <template slot-scope="scope">
                    <div class="cell-title">{{ scope.row.task_title }}</div>
                    <div class="cell-tips">任务ID：{{ scope.row.task_no }}</div>
                </template>
            </el-table-column>
            <el-table-column
                label="奖励金额"
                align="center"
                width="110"
                key="4"
            >
                <template slot-scope="scope">
                    <template v-if="filterData.task_category === 8">
                        {{ scope.row.task_price }}
                    </template>
                    <template v-else>
                        <el-popover
                            placement="bottom"
                            width="640"
                            trigger="click"
                        >
                            <div style="text-align: center;margin: 12px 0 24px;font-size: 16px">查看奖励设置</div>
                            <el-table :data="scope.row.award_config">
                                <el-table-column
                                    width="150"
                                    label="梯度等级"
                                    align="center"
                                >
                                    <template slot-scope="scope">
                                        梯度{{ scope.$index + 1 }}
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    property="require_submit_count"
                                    label="要求数量"
                                    align="center"
                                />
                                <el-table-column
                                    label="奖励类型"
                                    align="center"
                                >
                                    <template slot-scope="scope">
                                        {{ scope.row.type === 1 ? '现金奖励' : '0元购学生券' }}
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    label="奖励金额/数量"
                                    align="center"
                                    width="130"
                                >
                                    <template slot-scope="scope">
                                        {{ scope.row.total_num }}{{ scope.row.type === 1 ? '元' : '张' }}
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    label="用户端显示金额"
                                    align="center"
                                    width="130"
                                >
                                    <template slot-scope="scope">
                                        <div v-if="scope.row.type === 1">{{ scope.row.num }}元</div>
                                        <div v-else>--</div>
                                    </template>
                                </el-table-column>
                            </el-table>
                            <el-button
                                slot="reference"
                                type="text"
                            >查看奖励设置</el-button>
                        </el-popover>
                    </template>
                </template>
            </el-table-column>
            <el-table-column
                prop="show_task_price"
                label="抽佣后奖励"
                align="center"
                key="98"
                width="110"
                v-if="filterData.task_category === 8"
            />
            <el-table-column
                label="领取时间"
                width="160"
                key="5"
            >
                <template slot-scope="scope">
                    {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                </template>
            </el-table-column>
            <el-table-column
                label="反馈时间"
                width="160"
                key="99"
            >
                <template slot-scope="scope">
                    {{ scope.row.submit_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                </template>
            </el-table-column>
            <el-table-column
                label="任务状态"
                width="146"
                key="x"
            >
                <template slot-scope="scope">
                    <div
                        class="status-wrapper"
                        style="justify-content: flex-start"
                    >
                        <div
                            :class="['status-dot', scope.row.verify_status === 0 ? 'warning' : (scope.row.verify_status === 1 ? 'success' : scope.row.verify_status === -1 ? 'danger' : 'normal')]"/>
                        <div>{{ scope.row.verify_status | verifyStatusFilter }}</div>
                        <el-button
                            type="text"
                            v-if="scope.row.verify_status === -1"
                            @click="showReason(scope.row.verify_fail_reason)"
                        >
                            (查看原因)
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="奖励状态"
                align="center"
                width="110"
                key="7"
            >
                <template slot-scope="scope">
                    <div class="status-wrapper">
                        <div
                            :class="['status-dot', scope.row.award_status === 1 ? 'success' : scope.row.award_status === -1 ? 'danger' : 'normal']"/>
                        <div>{{ scope.row.award_status === 1 ? '已发放' : scope.row.award_status === -1 ? '拒绝发放' : '未发放' }}</div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="award_count"
                label="实际发放金额"
                show-overflow-tooltip
                align="center"
                width="120"
                :formatter="emptyFormatter"
                key="8"
                v-if="filterData.task_category === 7"
            />
            <el-table-column
                prop="verify_admin_true_name"
                label="操作人"
                show-overflow-tooltip
                align="center"
                :formatter="emptyFormatter"
                key="12"
            />
            <el-table-column
                prop="remark"
                label="备注"
                show-overflow-tooltip
                align="center"
                :formatter="emptyFormatter"
                key="13"
            />
            <el-table-column
                label="操作"
                align="left"
                width="160"
                fixed="right"
                key="14"
            >
                <template slot-scope="scope">
                    <div class="option-btn-group">
                        <template v-if="filterData.task_category === 7">
                            <el-button
                                type="text"
                                :disabled="scope.row.verify_status === 2"
                                @click="handleSetAward(scope.row)"
                            >
                                {{ scope.row.verify_status === 0 ? '审核' : '查看' }}
                            </el-button>
                        </template>
                        <template v-else>
                            <el-button
                                type="text"
                                :disabled="scope.row.verify_status === 2"
                                @click="handleRoute('standardTaskReceiveDetail', {
                                    apply_no: scope.row.apply_no
                                })"
                            >
                                {{ scope.row.verify_status === 0 ? '审核' : '查看' }}
                            </el-button>
                        </template>
                        <el-button
                            type="text"
                            @click="showRemarkDialog(scope.row)"
                        >
                            备注
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->
        <template slot="pagination">
            <div/>
            <el-pagination
                layout="total, prev, pager, next, jumper"
                background
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                v-if="page.total_count"
                @current-change="getTableData"
            />
        </template>
    </PageLayout>
</template>

<script>
import { back } from '@/mixin/back';
import {standardConfigDialog} from '@/components/standard-task/standard-config-dialog';
import ZdTabs from "@/components/zd-tabs";
import {verifyDialog} from "@/components/elite-task/grant-gradient-award-verify";

export default {
    name: 'StandardTaskReceiveList',
    components: {ZdTabs},
    mixins: [back],
    data() {
        return {
            filterData: {
                task_category: 7,
                student_keyword: '',
                task_keyword: '',
                school_name: '',
                verify_status: 99,
                award_status: 99,
                start_time: '',
                end_time: ''
            },
            dateTimeRange: null,
            tabsList: [
                {label: '推广引流', value: 7},
                {label: '问卷调研', value: 8},
            ],
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            statusList: [
                {label: '全部', value: 99},
                {label: '待审核', value: 0},
                {label: '已通过', value: 1},
                {label: '待提交', value: 2},
                {label: '未通过', value: -1},
                {label: '已放弃', value: -2},
            ],
            school_tree: []
        };
    },
    computed: {},
    filters: {
        statusFilter(val) {
            switch (val) {
                case 0:
                    return '进行中';
                case 1:
                    return '已完成';
                case -1:
                    return '未完成';
                case -2:
                    return '已放弃';
                default:
                    return '--';
            }
        },
        verifyStatusFilter(val) {
            switch (val) {
                case 0:
                    return '待审核';
                case 1:
                    return '已通过';
                case 2:
                    return '待提交';
                case -1:
                    return '未通过';
                case -2:
                    return '已放弃';
                case -3:
                    return '已终止';
                default:
                    return '--';
            }
        }
    },
    mounted() {
        let filterData = JSON.parse(sessionStorage.getItem('STANDARD_TASK_RECEIVE_QUERY'));

        if (filterData) {
            this.filterData = filterData;
            this.page.now_page = Number(sessionStorage.getItem('STANDARD_TASK_RECEIVE_PAGE_NO'));
            this.dateTimeRange = JSON.parse(sessionStorage.getItem('STANDARD_TASK_RECEIVE_DATE'));
            sessionStorage.removeItem('STANDARD_TASK_RECEIVE_QUERY');
            sessionStorage.removeItem('STANDARD_TASK_RECEIVE_PAGE_NO');
            sessionStorage.removeItem('STANDARD_TASK_RECEIVE_DATE');
        } else {
            this.filterData.task_category = this.$route.query.task_category || 7;
        }
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '工作'}, {title: '平台标准任务'}, {title: '任务领取列表'}]);
        this.authResult = this.$route.meta.authResult;
        this.filterData.task_keyword = this.$route.query.task_no ? this.$route.query.task_no : '';
        this.getTableData();
        this.getSchoolTree();
    },

    methods: {

        /**
         * @description 获取省/市/学校三级联动
         */
        getSchoolTree () {
            this.$post('/student/local_info%5Cschool_tree',{},resp => {
                if (resp.code === 1) {
                    this.school_tree = resp.data.tree;
                }
            });
        },

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            params.start_time = this.dateTimeRange ? this.dateTimeRange[0] / 1000 : '';
            params.end_time = this.dateTimeRange ? this.dateTimeRange[1] / 1000 : '';
            this.$post('/student/task_stuagent_student_task%5Cget_receive_task_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         * @param auth_name
         */
        handleRoute(routeName, query, auth_name) {
            if(auth_name && !this.authResult[auth_name]) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$router.push({
                name: routeName,
                query
            });

            sessionStorage.setItem('STANDARD_TASK_RECEIVE_QUERY', JSON.stringify(this.filterData));
            sessionStorage.setItem('STANDARD_TASK_RECEIVE_PAGE_NO', this.page.now_page);
            sessionStorage.setItem('STANDARD_TASK_RECEIVE_DATE', JSON.stringify(this.dateTimeRange));
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 备注
         * @param row
         */
        async showRemarkDialog(row) {
            let value = await this.$zdPrompt({
                title: '备注',
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                defaultValue: row.remark
            });

            if(value) {
                let params = {
                    apply_no: row.apply_no,
                    remark: value
                };

                this.$post('/student/task_stuagent_student_task%5Cset_remark', params, resp => {
                    if (resp.code === 1) {
                        this.$notify({
                            title: '提示',
                            message: '操作成功',
                            duration: 2000,
                            type: 'success'
                        });
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            }
        },

        /**
         * tab切换
         */
        handleTabChange() {
            this.getTableData(1);
            this.$nextTick(() => {
                this.$refs.table.doLayout();
            });
        },

        /**
         * 导出数据
         */
        handleExport() {
            if(!this.authResult.auth_export) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            let base = this.$baseUrl;
            let params = {...this.filterData};

            params.verify_status = 1;
            let ticket = localStorage.getItem('sessionTicket');
            let queryStr = Object.keys(params)
                .map(key => `${key}=${params[key]}`)
                .join('&');
            let url = `${base}/student/task_stuagent_student_task%5Cexport_step_task_report_list?${queryStr}&__ticket=${ticket}`;

            window.open(url);
        },

        /**
         * 显示驳回原因
         * @param reason
         */
        showReason(reason) {
            if(!reason) {
                this.$message.warning('驳回原因为空');
                return;
            }
            this.$zdPrompt({
                title: '审核未通过',
                showText: reason
            });
        },

        /**
         * 审核发放奖励
         * @param row
         * @param type
         */
        async handleSetAward(row, type) {
            let formData = await this.getDetail(row.apply_no);
            let result = await verifyDialog({
                formData,
                type: row.verify_status ? 2 : 1,
                status: row.verify_status
            });

            if(result) this.getTableData();
        },

        /**
         * 获取详情
         * @param apply_no
         * @returns {Promise<unknown>}
         */
        getDetail(apply_no) {
            this.dialogLoading = true;
            return new Promise(resolve => {
                this.$post('/student/task_stuagent_student_task%5Cget_verify_detail', {apply_no}, resp => {
                    if (resp.code === 1) {
                        resolve(resp.data);
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                    this.dialogLoading = false;
                });
            });
        },
    }
};
</script>

<style lang="less" scoped>
.standard-task-list-wrapper {
    /deep/ .content-wrapper {
        .header {
            display: none;
        }

        .filter-wrapper {
            display: initial !important;
            margin-top: 0 !important;
            padding: 0 !important;

            .filter-view {
                display: flex;
                flex-wrap: wrap;
                padding: 0 24px;
                margin-top: 24px;

                .left {
                    display: flex;
                    flex-wrap: wrap;
                    flex: 1;
                }

                .right {
                    width: 260px;
                    flex-shrink: 0;
                }
            }
        }
    }
}

</style>
